package com.phaeton.beemeup.util;

import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.PinchZoomDetector;

import android.util.Log;
import android.view.MotionEvent;

import com.phaeton.beemeup.GameManager;

public class TouchDetector implements IOnSceneTouchListener{
	private SmoothScrollDetector mScrollDetector;
	private PinchZoomDetector mZoomDetector;
	boolean bScrolled;
	private GameManager mGameManager;

	public TouchDetector(GameManager pGameManager){
		mGameManager = pGameManager;
		mScrollDetector = new SmoothScrollDetector(mGameManager.getCamera(), mGameManager.getLevel());
		mZoomDetector = new PinchZoomDetector(new PinchZoomer(mGameManager.getCamera()));
	}
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {	
    	MotionEvent event = pSceneTouchEvent.getMotionEvent();
    	this.mZoomDetector.onTouchEvent(pSceneTouchEvent);

		if(this.mZoomDetector.isZooming()) {
			this.mScrollDetector.setEnabled(false);
		} else {
			if(pSceneTouchEvent.isActionDown()) {
				this.mScrollDetector.setEnabled(true);
			}
			this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
		}
		
    	if(event.getAction() == MotionEvent.ACTION_DOWN){
        	bScrolled = false;
        	return true;
        }
    	
    	if(event.getAction() == MotionEvent.ACTION_UP && !bScrolled){
        	if(mGameManager.getSelectedBee() != null){
        		mGameManager.setSelectedBee(null); 
			}
        	return true;
        }
        
        if(event.getAction() == MotionEvent.ACTION_MOVE){
        	if(event.getEventTime() - event.getDownTime() > 100) bScrolled = true;
        	return true;
        }
				
		return false;
	}

}
